﻿using System;
using EPiServer.Filters;
using EPiServer.Core;

namespace EPiBoost.Core.Filters.PageFilters
{
    public class FilterLanguageBranch : IPageFilter
    {
        private string _languageBranch;
        public string LanguageBranch
        {
            get
            {
                if (string.IsNullOrEmpty(_languageBranch))
                {
                    _languageBranch = Helpers.HttpContextHelper.GetCurrentPageFromHttpContext().LanguageBranch;
                }
                return _languageBranch;
            }
            set { _languageBranch = value; }
        }
       
        public void Filter(PageDataCollection pages)
        {
            for (int i = pages.Count - 1; i >= 0; i--)
            {      
                if (pages[i].LanguageBranch != LanguageBranch)
                {
                    pages.RemoveAt(i);
                }
            }
        }

        public void Filter(object sender, FilterEventArgs e)
        {
            Filter(e.Pages);
        }

        public bool ShouldFilter(PageData page)
        {
            throw new NotImplementedException();
        }
    }
}